package cn.yll.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring 配置工具
 * @author yangll
 */
@Component
public class AppContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getContext() {
        return context;
    }

    public static <T> T getBean(Class<T> tClass) {
        return context.getBean(tClass);
    }

    public static <T> T getBean(String beanName, Class<T> tClass) {
        return context.getBean(beanName, tClass);
    }

    public static String getProperty(String key) {
        return context.getEnvironment().getProperty(key);
    }

    /**
     * 判断是否是生产环境
     */
    public static boolean isDeploy() {
        return "deploy".equals(AppContextUtil.getProperty("spring.profiles.active"));
    }
}
